package csel.view;

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.imageio.ImageIO;


public class ImageRepository {

	private static ImageRepository instance;
	
	private final HashMap<String,BufferedImage> imageMap;
	
	public static ImageRepository getInstance()
	{
		if (instance == null)
			instance = new ImageRepository();
		return instance;
	}
	
	private ImageRepository()
    {
            imageMap = new HashMap<String,BufferedImage>();
    }
	
	public BufferedImage get(String fileName)
	{
		if (imageMap.containsKey(fileName))
			return imageMap.get(fileName);
		else
		{
			File f = new File("images/"+fileName+".png");
			try
			{
				if (f.exists())
					imageMap.put(fileName,ImageIO.read(f));
				else 
				{
					imageMap.put(fileName, ImageIO.read(new File("images/default.png")));
				}
			}
			catch (IOException e)
			{
				System.out.println("[VIEW] Image read has failed!");
			}
			return imageMap.get(fileName);
		}   
	}

}